﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BMSQ.DBUtility;//Please add references
namespace BMSQ.DAL
{
	/// <summary>
	/// 数据访问类:Order_List
	/// </summary>
	public partial class Order_List
	{
		public Order_List()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string Order_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Order_List");
			strSql.Append(" where Order_ID=@Order_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Order_ID", SqlDbType.NVarChar,50)			};
			parameters[0].Value = Order_ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(BMSQ.Model.Order_List model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Order_List(");
			strSql.Append("Order_ID,Buyer,Vendor_ID,Order_Code,Created_Date,Delivery_Type,Delivery_Name,Delivery_Phone,Delivery_Address,Receive_PreReceiveDateTimeFrom,Receive_PreReceiveDateTimeTo,Vender_GetGoodsDateFrom,Vender_GetGoodsDateTo,Pay_Type,Pay_Status,Order_Status,Goods_Money,Delivery_Money,Pay_Date,Receive_Date,Closed_Date)");
			strSql.Append(" values (");
			strSql.Append("@Order_ID,@Buyer,@Vendor_ID,@Order_Code,@Created_Date,@Delivery_Type,@Delivery_Name,@Delivery_Phone,@Delivery_Address,@Receive_PreReceiveDateTimeFrom,@Receive_PreReceiveDateTimeTo,@Vender_GetGoodsDateFrom,@Vender_GetGoodsDateTo,@Pay_Type,@Pay_Status,@Order_Status,@Goods_Money,@Delivery_Money,@Pay_Date,@Receive_Date,@Closed_Date)");
			SqlParameter[] parameters = {
					new SqlParameter("@Order_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@Buyer", SqlDbType.NVarChar,50),
					new SqlParameter("@Vendor_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@Order_Code", SqlDbType.NVarChar,10),
					new SqlParameter("@Created_Date", SqlDbType.DateTime),
					new SqlParameter("@Delivery_Type", SqlDbType.Int,4),
					new SqlParameter("@Delivery_Name", SqlDbType.NVarChar,10),
					new SqlParameter("@Delivery_Phone", SqlDbType.NVarChar,20),
					new SqlParameter("@Delivery_Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Receive_PreReceiveDateTimeFrom", SqlDbType.DateTime),
					new SqlParameter("@Receive_PreReceiveDateTimeTo", SqlDbType.DateTime),
					new SqlParameter("@Vender_GetGoodsDateFrom", SqlDbType.DateTime),
					new SqlParameter("@Vender_GetGoodsDateTo", SqlDbType.DateTime),
					new SqlParameter("@Pay_Type", SqlDbType.Int,4),
					new SqlParameter("@Pay_Status", SqlDbType.Int,4),
					new SqlParameter("@Order_Status", SqlDbType.Int,4),
					new SqlParameter("@Goods_Money", SqlDbType.Decimal,9),
					new SqlParameter("@Delivery_Money", SqlDbType.Decimal,9),
					new SqlParameter("@Pay_Date", SqlDbType.DateTime),
					new SqlParameter("@Receive_Date", SqlDbType.DateTime),
					new SqlParameter("@Closed_Date", SqlDbType.DateTime)};
			parameters[0].Value = model.Order_ID;
			parameters[1].Value = model.Buyer;
			parameters[2].Value = model.Vendor_ID;
			parameters[3].Value = model.Order_Code;
			parameters[4].Value = model.Created_Date;
			parameters[5].Value = model.Delivery_Type;
			parameters[6].Value = model.Delivery_Name;
			parameters[7].Value = model.Delivery_Phone;
			parameters[8].Value = model.Delivery_Address;
			parameters[9].Value = model.Receive_PreReceiveDateTimeFrom;
			parameters[10].Value = model.Receive_PreReceiveDateTimeTo;
			parameters[11].Value = model.Vender_GetGoodsDateFrom;
			parameters[12].Value = model.Vender_GetGoodsDateTo;
			parameters[13].Value = model.Pay_Type;
			parameters[14].Value = model.Pay_Status;
			parameters[15].Value = model.Order_Status;
			parameters[16].Value = model.Goods_Money;
			parameters[17].Value = model.Delivery_Money;
			parameters[18].Value = model.Pay_Date;
			parameters[19].Value = model.Receive_Date;
			parameters[20].Value = model.Closed_Date;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(BMSQ.Model.Order_List model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Order_List set ");
			strSql.Append("Buyer=@Buyer,");
			strSql.Append("Vendor_ID=@Vendor_ID,");
			strSql.Append("Order_Code=@Order_Code,");
			strSql.Append("Created_Date=@Created_Date,");
			strSql.Append("Delivery_Type=@Delivery_Type,");
			strSql.Append("Delivery_Name=@Delivery_Name,");
			strSql.Append("Delivery_Phone=@Delivery_Phone,");
			strSql.Append("Delivery_Address=@Delivery_Address,");
			strSql.Append("Receive_PreReceiveDateTimeFrom=@Receive_PreReceiveDateTimeFrom,");
			strSql.Append("Receive_PreReceiveDateTimeTo=@Receive_PreReceiveDateTimeTo,");
			strSql.Append("Vender_GetGoodsDateFrom=@Vender_GetGoodsDateFrom,");
			strSql.Append("Vender_GetGoodsDateTo=@Vender_GetGoodsDateTo,");
			strSql.Append("Pay_Type=@Pay_Type,");
			strSql.Append("Pay_Status=@Pay_Status,");
			strSql.Append("Order_Status=@Order_Status,");
			strSql.Append("Goods_Money=@Goods_Money,");
			strSql.Append("Delivery_Money=@Delivery_Money,");
			strSql.Append("Pay_Date=@Pay_Date,");
			strSql.Append("Receive_Date=@Receive_Date,");
			strSql.Append("Closed_Date=@Closed_Date");
			strSql.Append(" where Order_ID=@Order_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Buyer", SqlDbType.NVarChar,50),
					new SqlParameter("@Vendor_ID", SqlDbType.NVarChar,50),
					new SqlParameter("@Order_Code", SqlDbType.NVarChar,10),
					new SqlParameter("@Created_Date", SqlDbType.DateTime),
					new SqlParameter("@Delivery_Type", SqlDbType.Int,4),
					new SqlParameter("@Delivery_Name", SqlDbType.NVarChar,10),
					new SqlParameter("@Delivery_Phone", SqlDbType.NVarChar,20),
					new SqlParameter("@Delivery_Address", SqlDbType.NVarChar,200),
					new SqlParameter("@Receive_PreReceiveDateTimeFrom", SqlDbType.DateTime),
					new SqlParameter("@Receive_PreReceiveDateTimeTo", SqlDbType.DateTime),
					new SqlParameter("@Vender_GetGoodsDateFrom", SqlDbType.DateTime),
					new SqlParameter("@Vender_GetGoodsDateTo", SqlDbType.DateTime),
					new SqlParameter("@Pay_Type", SqlDbType.Int,4),
					new SqlParameter("@Pay_Status", SqlDbType.Int,4),
					new SqlParameter("@Order_Status", SqlDbType.Int,4),
					new SqlParameter("@Goods_Money", SqlDbType.Decimal,9),
					new SqlParameter("@Delivery_Money", SqlDbType.Decimal,9),
					new SqlParameter("@Pay_Date", SqlDbType.DateTime),
					new SqlParameter("@Receive_Date", SqlDbType.DateTime),
					new SqlParameter("@Closed_Date", SqlDbType.DateTime),
					new SqlParameter("@Order_ID", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.Buyer;
			parameters[1].Value = model.Vendor_ID;
			parameters[2].Value = model.Order_Code;
			parameters[3].Value = model.Created_Date;
			parameters[4].Value = model.Delivery_Type;
			parameters[5].Value = model.Delivery_Name;
			parameters[6].Value = model.Delivery_Phone;
			parameters[7].Value = model.Delivery_Address;
			parameters[8].Value = model.Receive_PreReceiveDateTimeFrom;
			parameters[9].Value = model.Receive_PreReceiveDateTimeTo;
			parameters[10].Value = model.Vender_GetGoodsDateFrom;
			parameters[11].Value = model.Vender_GetGoodsDateTo;
			parameters[12].Value = model.Pay_Type;
			parameters[13].Value = model.Pay_Status;
			parameters[14].Value = model.Order_Status;
			parameters[15].Value = model.Goods_Money;
			parameters[16].Value = model.Delivery_Money;
			parameters[17].Value = model.Pay_Date;
			parameters[18].Value = model.Receive_Date;
			parameters[19].Value = model.Closed_Date;
			parameters[20].Value = model.Order_ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string Order_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Order_List ");
			strSql.Append(" where Order_ID=@Order_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Order_ID", SqlDbType.NVarChar,50)			};
			parameters[0].Value = Order_ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Order_IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Order_List ");
			strSql.Append(" where Order_ID in ("+Order_IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BMSQ.Model.Order_List GetModel(string Order_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Order_ID,Buyer,Vendor_ID,Order_Code,Created_Date,Delivery_Type,Delivery_Name,Delivery_Phone,Delivery_Address,Receive_PreReceiveDateTimeFrom,Receive_PreReceiveDateTimeTo,Vender_GetGoodsDateFrom,Vender_GetGoodsDateTo,Pay_Type,Pay_Status,Order_Status,Goods_Money,Delivery_Money,Pay_Date,Receive_Date,Closed_Date from Order_List ");
			strSql.Append(" where Order_ID=@Order_ID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Order_ID", SqlDbType.NVarChar,50)			};
			parameters[0].Value = Order_ID;

			BMSQ.Model.Order_List model=new BMSQ.Model.Order_List();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BMSQ.Model.Order_List DataRowToModel(DataRow row)
		{
			BMSQ.Model.Order_List model=new BMSQ.Model.Order_List();
			if (row != null)
			{
				if(row["Order_ID"]!=null)
				{
					model.Order_ID=row["Order_ID"].ToString();
				}
				if(row["Buyer"]!=null)
				{
					model.Buyer=row["Buyer"].ToString();
				}
				if(row["Vendor_ID"]!=null)
				{
					model.Vendor_ID=row["Vendor_ID"].ToString();
				}
				if(row["Order_Code"]!=null)
				{
					model.Order_Code=row["Order_Code"].ToString();
				}
				if(row["Created_Date"]!=null && row["Created_Date"].ToString()!="")
				{
					model.Created_Date=DateTime.Parse(row["Created_Date"].ToString());
				}
				if(row["Delivery_Type"]!=null && row["Delivery_Type"].ToString()!="")
				{
					model.Delivery_Type=int.Parse(row["Delivery_Type"].ToString());
				}
				if(row["Delivery_Name"]!=null)
				{
					model.Delivery_Name=row["Delivery_Name"].ToString();
				}
				if(row["Delivery_Phone"]!=null)
				{
					model.Delivery_Phone=row["Delivery_Phone"].ToString();
				}
				if(row["Delivery_Address"]!=null)
				{
					model.Delivery_Address=row["Delivery_Address"].ToString();
				}
				if(row["Receive_PreReceiveDateTimeFrom"]!=null && row["Receive_PreReceiveDateTimeFrom"].ToString()!="")
				{
					model.Receive_PreReceiveDateTimeFrom=DateTime.Parse(row["Receive_PreReceiveDateTimeFrom"].ToString());
				}
				if(row["Receive_PreReceiveDateTimeTo"]!=null && row["Receive_PreReceiveDateTimeTo"].ToString()!="")
				{
					model.Receive_PreReceiveDateTimeTo=DateTime.Parse(row["Receive_PreReceiveDateTimeTo"].ToString());
				}
				if(row["Vender_GetGoodsDateFrom"]!=null && row["Vender_GetGoodsDateFrom"].ToString()!="")
				{
					model.Vender_GetGoodsDateFrom=DateTime.Parse(row["Vender_GetGoodsDateFrom"].ToString());
				}
				if(row["Vender_GetGoodsDateTo"]!=null && row["Vender_GetGoodsDateTo"].ToString()!="")
				{
					model.Vender_GetGoodsDateTo=DateTime.Parse(row["Vender_GetGoodsDateTo"].ToString());
				}
				if(row["Pay_Type"]!=null && row["Pay_Type"].ToString()!="")
				{
					model.Pay_Type=int.Parse(row["Pay_Type"].ToString());
				}
				if(row["Pay_Status"]!=null && row["Pay_Status"].ToString()!="")
				{
					model.Pay_Status=int.Parse(row["Pay_Status"].ToString());
				}
				if(row["Order_Status"]!=null && row["Order_Status"].ToString()!="")
				{
					model.Order_Status=int.Parse(row["Order_Status"].ToString());
				}
				if(row["Goods_Money"]!=null && row["Goods_Money"].ToString()!="")
				{
					model.Goods_Money=decimal.Parse(row["Goods_Money"].ToString());
				}
				if(row["Delivery_Money"]!=null && row["Delivery_Money"].ToString()!="")
				{
					model.Delivery_Money=decimal.Parse(row["Delivery_Money"].ToString());
				}
				if(row["Pay_Date"]!=null && row["Pay_Date"].ToString()!="")
				{
					model.Pay_Date=DateTime.Parse(row["Pay_Date"].ToString());
				}
				if(row["Receive_Date"]!=null && row["Receive_Date"].ToString()!="")
				{
					model.Receive_Date=DateTime.Parse(row["Receive_Date"].ToString());
				}
				if(row["Closed_Date"]!=null && row["Closed_Date"].ToString()!="")
				{
					model.Closed_Date=DateTime.Parse(row["Closed_Date"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Order_ID,Buyer,Vendor_ID,Order_Code,Created_Date,Delivery_Type,Delivery_Name,Delivery_Phone,Delivery_Address,Receive_PreReceiveDateTimeFrom,Receive_PreReceiveDateTimeTo,Vender_GetGoodsDateFrom,Vender_GetGoodsDateTo,Pay_Type,Pay_Status,Order_Status,Goods_Money,Delivery_Money,Pay_Date,Receive_Date,Closed_Date ");
			strSql.Append(" FROM Order_List ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Order_ID,Buyer,Vendor_ID,Order_Code,Created_Date,Delivery_Type,Delivery_Name,Delivery_Phone,Delivery_Address,Receive_PreReceiveDateTimeFrom,Receive_PreReceiveDateTimeTo,Vender_GetGoodsDateFrom,Vender_GetGoodsDateTo,Pay_Type,Pay_Status,Order_Status,Goods_Money,Delivery_Money,Pay_Date,Receive_Date,Closed_Date ");
			strSql.Append(" FROM Order_List ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Order_List ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Order_ID desc");
			}
			strSql.Append(")AS Row, T.*  from Order_List T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Order_List";
			parameters[1].Value = "Order_ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

